﻿using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Enterprise.Utility;//请先添加引用
namespace Enterprise.Dal
{
    /// <summary>
    /// 数据访问类DArticle_Type。
    /// </summary>
    public class DArticle_Type
    {
        public DArticle_Type()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ArticleTypeID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 from Article_Type");
            strSql.Append(" where ArticleTypeID=@ArticleTypeID");
            SqlParameter[] parameters = {
					new SqlParameter("@ArticleTypeID", SqlDbType.Int,4)
};
            parameters[0].Value = ArticleTypeID;

            return DbHelperSQL.ExecuteScalar(strSql.ToString(), parameters).GetInt(0) == 0 ? false : true;
        }


        /// <summary>
        ///增加一条数据
        /// </summary>
        public int Add(Enterprise.Model.Article_Type model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into Article_Type(");
            strSql.Append("ArticleTypeName)");
            strSql.Append(" values (");
            strSql.Append("@ArticleTypeName)");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
					new SqlParameter("@ArticleTypeName", SqlDbType.NVarChar,20)};
            parameters[0].Value = model.ArticleTypeName;

            return DbHelperSQL.ExecuteScalar(strSql.ToString(), parameters).GetInt(0, false);
        }
        /// <summary>
        ///更新一条书籍
        /// </summary>
        public bool Update(Enterprise.Model.Article_Type model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Article_Type set ");
            strSql.Append("ArticleTypeName=@ArticleTypeName");
            strSql.Append(" where ArticleTypeID=@ArticleTypeID");
            SqlParameter[] parameters = {
					new SqlParameter("@ArticleTypeID", SqlDbType.Int,4),
					new SqlParameter("@ArticleTypeName", SqlDbType.NVarChar,20)};
            parameters[0].Value = model.ArticleTypeID;
            parameters[1].Value = model.ArticleTypeName;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ArticleTypeID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Article_Type ");
            strSql.Append(" where ArticleTypeID=@ArticleTypeID");
            SqlParameter[] parameters = {
					new SqlParameter("@ArticleTypeID", SqlDbType.Int,4)
};
            parameters[0].Value = ArticleTypeID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
        /// <summary>
        ///批量删除
        /// </summary>
        public bool DeleteList(string ArticleTypeIDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from Article_Type ");
            strSql.Append(" where ArticleTypeID in (" + ArticleTypeIDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        ///获取实体
        /// </summary>
        public Enterprise.Model.Article_Type GetModel(int ArticleTypeID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 ArticleTypeID,ArticleTypeName from Article_Type ");
            strSql.Append(" where ArticleTypeID=@ArticleTypeID");
            SqlParameter[] parameters = {
					new SqlParameter("@ArticleTypeID", SqlDbType.Int,4)
};
            parameters[0].Value = ArticleTypeID;

            Enterprise.Model.Article_Type model = new Enterprise.Model.Article_Type();
            DataSet ds = DbHelperSQL.ExecuteDataSet(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ArticleTypeID"].ToString() != "")
                {
                    model.ArticleTypeID = int.Parse(ds.Tables[0].Rows[0]["ArticleTypeID"].ToString());
                }
                model.ArticleTypeName = ds.Tables[0].Rows[0]["ArticleTypeName"].ToString();
                return model;
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        ///得到GetList
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ArticleTypeID,ArticleTypeName ");
            strSql.Append(" FROM Article_Type ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 得到GetList
        /// </summary>
        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ");
            if (Top > 0)
            {
                strSql.Append(" top " + Top.ToString());
            }
            strSql.Append(" ArticleTypeID,ArticleTypeName ");
            strSql.Append(" FROM Article_Type ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(strSql.ToString());
        }


        #endregion  成员方法
    }
}

